/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server;

import Model.Category;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class ServerCategory {
    
    ObjectInputStream inObject;
    ObjectOutputStream outObject;
    
    ServerCategory(ObjectInputStream inObject, ObjectOutputStream outObject) {
         this.inObject = inObject;
        this.outObject =outObject;
    }
    public ServerCategory()
    {
        
    }
    public void controlAddCategory(){
        try {
            String name=(String)inObject.readObject();
            if(addCategory(name)){
                outObject.writeObject("Add category completed");
            }
            else{
                outObject.writeObject("Add category not completed. Please try again");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    public void editCategory()
    {
            try {
              Category cate = (Category) inObject.readObject();//nhan doi tuong login do client goi         
              outObject.writeObject(EditCategory(cate));      //goi thong bao cho client                
            } catch (IOException | ClassNotFoundException ex) {
                    System.out.println(ex);
            }
    }

    public boolean addCategory(String categoryName) {


        try {

            int i = -1;//Luu so dong cua bang Category

            Connection con = MyConnect.getConnect();
            String sql1 = "select CategoryID from Category";
            String sql2 = "insert into Category values(?,?)";
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql1);

            while (rs.next()) {
                i++;
            }
            PreparedStatement pst = con.prepareStatement(sql2);
            pst.setInt(1, ++i);    //Tang chi so hang len 1 de them vao;
            pst.setString(2, categoryName);
            pst.executeUpdate();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "addCategory " + e.getMessage());
            return false;
        }

    }

    public boolean EditCategory(Category cate) {
        int i = 0;
        try {
            Connection con = MyConnect.getConnect();
            String sql = "update Category set CategoryName=? where CategoryID=?";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, cate.getCategoryName());
            pst.setString(2, cate.getCategoryID());
            pst.executeUpdate();
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "editCategory " + e.getMessage());
            return false;
        }

    }

    public String returnCategoryID(String categoryName) {    //Danh cho tim kiem theo Category
        Connection con = MyConnect.getConnect();
        try {
            Statement stm = con.createStatement();
            String sql = "select CategoryID from Category where CategoryName='" + categoryName + "'";
            ResultSet rs = stm.executeQuery(sql);
            if (rs.next()) {
                return rs.getString(1);
            }else{
                JOptionPane.showMessageDialog(null, "No data category");
                return null;
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "returnCategoryID: " + ex.getMessage());
            return null;
        }
    }

    public String returnCategoryName(String categoryID) {    //Danh cho hien thong tin sach
        Connection con = MyConnect.getConnect();
        try {
            Statement stm = con.createStatement();
            String sql = "select CategoryName from Category where CategoryID='" + categoryID + "'";
            ResultSet rs = stm.executeQuery(sql);
            rs.next();
            return rs.getString(1);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "returnCategoryName " + ex.getMessage());
            return null;
        }
    }

    public ArrayList<String> getDataCategoryName() { //Danh cho combobox
        ArrayList<String> tbCategoryName = new ArrayList<>();
        Connection con = MyConnect.getConnect();
        String sql = "select CategoryName from Category";
        try {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()) {
                tbCategoryName.add(rs.getString(1));
            }
            return tbCategoryName;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "getDate Category " + ex.getMessage());
        }
        return null;

    }
}
